﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Reflection;
using FinPlusCompCore;
using QLNet;

namespace FinPlusCompQuant
{
    public class Trade :FinPlusComponent
    {
        public virtual Instrument Underlying { get; set; }

        //public
        public virtual void SetPricingEngine(IPricingEngine engine)
        {
            Underlying.setPricingEngine(engine);
        }

        public virtual object[] Value(string[] controls)
        {
            var type = Underlying.GetType();
            var res = new object[controls.Length];
            for (var i = 0; i < controls.Length; i++)
                res[i] = Invoke(type, controls[i]);

            return res;
        }

        public virtual object Invoke(Type type, string control, double factor = 1)
        {
            try
            {

                MethodInfo methodInfo = type.GetMethod(control);
                if (methodInfo == null)
                    return "NA";

                var res = methodInfo.Invoke(Underlying, null);

                if (Equals("NPV"))
                    res = factor * (double)res;

                return res;
            }
            catch (Exception e)
            {
                return (object)e.Message;
            }
        }
    }
}
